package kk.service.impl;

import com.github.pagehelper.PageHelper;
import kk.dao.ProductDao;
import kk.domain.Product;
import kk.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public List<Product> findAll() throws Exception {
        List<Product> products = productDao.findAll();
        return products;
    }

    @Override
    public List<Product> findAll(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> products = productDao.findAll();
        return products;
    }

    @Override
    public Product findById(Integer id) throws Exception {
        Product product = productDao.findById(id);
        return product;
    }

    @Override
    public void save(Product product) throws Exception {
        productDao.save(product);
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        productDao.deleteById(id);
    }

    @Override
    public void deleteByIds(int[] ids) throws Exception {
        for (int id : ids) {
            productDao.deleteById(id);
        }
    }

    @Override
    public void update(Product product) throws Exception {
        productDao.update(product);
    }

    @Override
    public List<Product> findProductByLevel(String level) throws Exception {
        //如果等级为C
        if ("C".equalsIgnoreCase(level)){
            return productDao.findProductByPrice(0, 2999);
        }else if ("B".equalsIgnoreCase(level)){
            return productDao.findProductByPrice(3000,5000);
        }else {
            return productDao.findProductByPrice(5001,99999);
        }
    }

    @Override
    public List<Product> findByName(Integer pageNum, Integer pageSize, String productName) throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productDao.findByName(productName);
        return productList;
    }

}
